#pragma once
#include "ctp/ThostFtdcTraderApi.h"
// #include "ctp/ThostFtdcUserApiStruct.h"
#include <string>
#include <iostream>
#include <ctime>
#include <nlohmann/json.hpp>
#include "tcp_server.h"
#include "logger.h"
using json = nlohmann::json;

extern int nRequestID;

class CTraderHandler : public CThostFtdcTraderSpi {
private:
    CThostFtdcTraderApi* m_ptraderapi;
    std::string m_brokerID;  // 9999
    std::string m_userID;  // 133492
    std::string m_password;  // Whnh@868881
    std::string m_flow_path; //.//flow/
    std::string m_front_address; //tcp://180.168.146.187:10201
    std::string order_id;
    std::string m_app_id;
    std::string m_auth_code;
    bool m_connected = false;
    bool m_logged_in = false;
    bool m_settlement_confirmed = false;
    TcpServer* m_tcp_server;

public:
    // 添加构造函数声明
    CTraderHandler(const std::string& brokerID, const std::string& userID, 
                  const std::string& password, const std::string& flow_path, 
                  const std::string& front_address, const std::string& app_id = "", 
                  const std::string& auth_code = "", TcpServer* m_tcp_server = nullptr);
    
    void connect();
    void release();
    void login();
    void settlementinfoConfirm();
    int orderInsert(
        const std::string& instrument,
        const std::string& exchange,
        const std::string& direction,
        const double price,
        const int volume,
        const std::string& offect,
        const std::string& order_ref);
    int orderCancel(const std::string& instrument, const std::string& exchange, const std::string& sid);
    void qryInstrument(const std::string& exchange="");
    int getOrder(const std::string& exchange="");
    int getAccount();
    int getPosition();
    int getTrade();

    bool isReady() const {
        std::cout << "m_ptraderapi: " << (m_ptraderapi ? "已初始化" : "未初始化") << std::endl;
        std::cout << "m_connected: " << (m_connected ? "已连接" : "未连接") << std::endl;
        std::cout << "m_logged_in: " << (m_logged_in ? "已登录" : "未登录") << std::endl;
        std::cout << "m_settlement_confirmed: " << (m_settlement_confirmed ? "已确认" : "未确认") << std::endl; 
        return m_ptraderapi && m_connected && m_logged_in && m_settlement_confirmed;
    }

    // 回调函数声明
    virtual void OnFrontConnected();
    virtual void OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
    virtual void OnRspSettlementInfoConfirm(CThostFtdcSettlementInfoConfirmField *pSettlementInfoConfirm, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
    virtual void OnRspQryInstrument(CThostFtdcInstrumentField *pInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
    virtual void OnRtnOrder(CThostFtdcOrderField *pOrder);
    virtual void OnRtnTrade(CThostFtdcTradeField *pTrade);
    virtual void OnRspOrderInsert(CThostFtdcInputOrderField *pInputOrder, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
    virtual void OnRspError(CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
    virtual void OnRspQryInvestorPosition(CThostFtdcInvestorPositionField *pInvestorPosition, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
    virtual void OnRspQryOrder(CThostFtdcOrderField *pOrder, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
    virtual void OnRspQryTrade(CThostFtdcTradeField *pTrade, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
    virtual void OnRspQryTradingAccount(CThostFtdcTradingAccountField *pTradingAccount, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
    void setServer(TcpServer* server);
    virtual ~CTraderHandler() {} // 添加虚析构函数
    virtual void OnRspAuthenticate(CThostFtdcRspAuthenticateField *pRspAuthenticateField, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
    void ReqAuthenticate();
    virtual void OnFrontDisconnected(int nReason);
};